/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jaotwinker;

import java.awt.Color;
import java.io.IOException;
import java.awt.Graphics2D;
import java.awt.event.*;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import java.util.ArrayList;
//import java.
/**
 *
 * @author Grayson
 */
public class Slot extends javax.swing.JLabel implements MouseListener, ActionListener {
    int x,y;
    String name;
    String tab;
    String search;
    java.awt.image.BufferedImage img;
    JPopupMenu popup;
    ArrayList items;
    int selectedItem;
    MainWindow window;
    
    public Slot(int x, int y, String name, String tab, MainWindow window) {
        this.x=x; this.y=y; this.name=name; this.tab=tab; this.window=window;
        this.setBounds(x,y,52,52);
        selectedItem=-1;
        //changeIcon(273470); // 273470
        clear();
        this.addMouseListener(this);
    }
    
    public void paintComponent(java.awt.Graphics g) {
        java.awt.Graphics2D g2 = (Graphics2D)g;
        double scale = 0.90;
        if(img!=null) {
            int w = (int)(scale*img.getWidth(this));
            int h = (int)(scale*img.getHeight(this));
            g2.drawImage(img, 0, 0, w, h, this);
        }
        else {
            g2.setColor(Color.BLACK);
            g2.fillRect(0,0,52, 52);
        }
        g2.dispose();
    }
    
    public void changeIcon(int id) {
        img=null; this.setVisible(false);
        java.net.URL url = getClass().getResource("/jaotwinker/resources/Icons/"+id+".gif");
        try {
            img = javax.imageio.ImageIO.read(url);
            this.setVisible(true);
        } catch(java.io.IOException e) { System.out.println(id + " not found."); 
        } catch(IllegalArgumentException e) { System.out.println(id + "."); }
    }
    
    public void clear() {
        items = new ArrayList();
        img=null;
        this.setVisible(false);
        popup=null;
    }
    
    public void selectItem(int index) {
        if(items.size()>=index) {
            selectedItem=index;
            Item i = (Item)items.get(selectedItem);
            changeIcon(i.icon);
        }
        else { System.out.println("fAIL"); }
    }
    
    public void sort(String skill) {
        ItemCompare ic; 
        if(skill.equalsIgnoreCase("SkillLockModifier") || skill.equalsIgnoreCase("NPCostModifier")) {
            ic = new ItemCompare(true); 
        }
        else ic = new ItemCompare(false);
        ic.skill = skill;
        java.util.Collections.sort(items, ic);
    }
    
    public void shrinkImplants() {
        if(!tab.equalsIgnoreCase("Implant")) return;
        java.util.Iterator it = items.iterator();
        /*while(it.hasNext()) {
            Item i = (Item)it.next();
            System.out.println(i.QL);
        }*/
        ArrayList newList = new ArrayList();
        java.util.Iterator it1 = items.iterator();
        java.util.Iterator it2;
        boolean addedHighestImp=false;
        for(int i=0;i<items.size();i++) {
            Item i1 = (Item)items.get(i);
            if(!i1.Name.contains("Implant")) {
                newList.add(i1);
                continue;
            }
            if(!addedHighestImp) { newList.add(i1); addedHighestImp=true; continue; } // add the highest implant
            boolean add=true;
            for(int j=0;j<newList.size();j++) {
                if(i==j) continue;
                Item i2 = (Item)newList.get(j);
                if(i1.Name.contains("Implant") && i2.Name.contains("Implant") && i1.QL==i2.QL) { add=false; }
            }
            if(add) newList.add(i1);
        }
        items = newList;
    }
    
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {
        if(img!=null && selectedItem>=0) {
            Item i = (Item)items.get(selectedItem);
            this.setToolTipText(i.Name);
        }
    }
    public void mouseExited(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {
        if(e.getButton()==MouseEvent.BUTTON3) {
            popup = new JPopupMenu();
            popup.setAutoscrolls(true);
            
            for(int i=0;i<items.size();i++) {
                Item item = (Item)items.get(i);
                JMenuItem j = new JMenuItem("("+item.getSkill(search)+") QL"+item.QL+" "+item.Name);
                j.setActionCommand(String.valueOf(i));
                j.addActionListener(this);
                java.awt.Font font = new java.awt.Font("LucidaSansRegular",0,10);
                j.setFont(font);
                popup.add(j);
                
            }
            JMenuItem j = new JMenuItem("Empty");
            java.awt.Font font = new java.awt.Font("LucidaSansRegular",0,10);
            j.setActionCommand("-1");
            j.addActionListener(this);
            j.setFont(font);
            popup.add(j);
            popup.show(e.getComponent(), e.getX(), e.getY());
            //popup.setSize(new java.awt.Dimension(100,100));
        }
        else if(e.getButton()==MouseEvent.BUTTON2) {
            if(window.excludeMode) {
               Item item = (Item)items.get(selectedItem);
               window.ei.add(item.AOID);
               window.DisplayMessage("Eliminated " + item.AOID + " " + item.Name);
            }
        }
        else if(e.getButton()==MouseEvent.BUTTON1) {
            try {
                Item item = (Item)items.get(selectedItem);
                String url = "http://www.xyphos.com/ao/aodb.php?id=" + item.AOID;
                java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
            }
            catch (java.io.IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        int i = Integer.parseInt(e.getActionCommand());
        if(i>=0) selectItem(i);
        else {
            selectedItem=-1;
            changeIcon(121302);
        }
    }
}